<template>
	<view>
		<uni-table :loading="isLoading" border stripe>
			<uni-tr>
				<uni-th align="center">名称</uni-th>
				<uni-th align="center">价格上浮次数</uni-th>
				<uni-th width="center" align="center">初始价格</uni-th>
				<uni-th width="center" align="center">回收价格</uni-th>
				<!-- <uni-th width="center" align="center">春</uni-th>
				<uni-th width="center" align="center">夏</uni-th>
				<uni-th width="center" align="center">秋</uni-th>
				<uni-th width="center" align="center">冬</uni-th> -->
				<uni-th width="center" align="center">操作</uni-th>
			</uni-tr>
			<uni-tr v-for="(item,index) in data" :key="index">
				<uni-td align="center">{{item.name}}</uni-td>
				<uni-td align="center">{{item.fengding}}</uni-td>
				<uni-td align="center">{{item.price}}</uni-td>
				<uni-td align="center">{{item.huishou}}</uni-td>
				<!-- <uni-td align="center">{{item.cal1}}</uni-td>
				<uni-td align="center">{{item.cal2}}</uni-td>
				<uni-td align="center">{{item.cal3}}</uni-td>
				<uni-td align="center">{{item.cal4}}</uni-td> -->
				<uni-td align="center">
					<view class="uni-group">
						<button @click="update(item)" class="uni-button" size="mini" type="primary">修改</button>
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>
		<uni-popup ref="passwordPopup" type="center">
			<view class="modal" style="width:450px; padding: 20px;">
				
				<view class="uni-container">
					<uni-forms ref="form" validateTrigger="bind">
						<uni-forms-item label="名称" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="名称" v-model="changedata.name"/>
						</uni-forms-item>
						<uni-forms-item label="价格上浮次数" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="价格上浮次数" v-model="changedata.fengding"/>
						</uni-forms-item>
						<uni-forms-item label="初始价格" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="初始价格" v-model="changedata.price"/>
						</uni-forms-item>
						<uni-forms-item label="回收价格" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="回收价格" v-model="changedata.huishou"/>
						</uni-forms-item>
						<!-- <uni-forms-item label="春" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="春" v-model="changedata.cal1"/>
						</uni-forms-item>
						<uni-forms-item label="夏" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="夏" v-model="changedata.cal2"/>
						</uni-forms-item>
						<uni-forms-item label="秋" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="秋" v-model="changedata.cal3"/>
						</uni-forms-item>
						<uni-forms-item label="冬" name="title" labelWidth="100">
							<input class="uni-input-border" type="text" placeholder="冬" v-model="changedata.cal4"/>
						</uni-forms-item> -->
						
						<view class="uni-button-group pointer">
							<button class="uni-button uni-button-full" type="primary" :disabled="isLoading" @click="save">保存</button>
						</view>
					</uni-forms>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoading:false,
				data:[],
				changedata:{
					cal4:"",
					cal3:"",
					cal2:"",
					cal1:"",
					name:"",
					fengding:"",
					huishou:"",
					price:""
				}
			}
		},
		methods: {
			getcats(){
				var self = this
				this.isLoading = true
				self.data = []
				this.$request('admin/loadconfig',{id:'cats'}).then(res => {
					this.isLoading = false
					var json = res.config
					console.log(json)
					for(let key in json){
						if(key != '_id'){
							var data = json[key]
							data['key'] = key
							self.data.push(data)
						}
					}
				}).catch(res => {
				
				}).finally(err => {
					this.isLoading = false
				})
			},
			update(data){
				var self = this
				for(let key in data){
					self.changedata[key] = data[key]
				}
				console.log(self.changedata)
				this.$refs.passwordPopup.open()
			},
			save(){
				var self = this
				self.isLoading = true
				var json = {}
				var data = {}
                for(var i=0;i<self.data.length;i++){
                    var item = self.data[i]
                    var key = item['key']
                    delete item['key']
                    json[key] = item
                }
				for(let key in self.changedata){
					if(key != 'key'){
						data[key] = self.changedata[key]
					}
				}
				json[self.changedata['key']] = data
                
				self.$request('admin/changecat',{json:JSON.stringify(json)}).then(res => {
					self.isLoading = false
					uni.showToast({
						title: "修改成功",
						icon: 'none',
						duration: 2000
					})
					this.$refs.passwordPopup.close()
					self.getcats()
				}).catch(res => {
				
				}).finally(err => {
					this.isLoading = false
				})
			},
			getcats1(){
				var self = this
				this.isLoading = true
				this.$request('admin/loadconfig1').then(res => {
					this.isLoading = false
					var json = res
					console.log(json)
					self.$request('admin/setconfig',{json:JSON.stringify(res.config)})
				}).catch(res => {
				
				}).finally(err => {
					this.isLoading = false
				})
			},
		},
		mounted() {
			var self = this
			self.getcats()
		}
	}
</script>

<style>
	.modal {
		width: 100%;
		max-width: 980px;
		margin: 0 auto;
		background-color: #FFFFFF;
	}
	
	.modal-content {
		padding: 15px;
		height: 500px;
		box-sizing: border-box;
	}
	
	.password-popup {
		padding: 30px;
	}
</style>
